# -*- coding:utf-8 -*-
"""
@FileName：Bias.py
@Description：
@Author：fdguuwzj
@Time：2024-01-17 20:24
"""
import numpy as np


def signal(*args):
    df = args[0]
    n = args[1]
    factor_name = args[2]

    df['ma'] = df['close'].rolling(n).mean()

    # 1.计算原始的因子
    df['_factor1h'] = (df['close'] - df['ma']) / df['ma']

    # 2.获取小时
    df['hour'] = df['candle_begin_time'].dt.hour
    # 3.单独分离出周期为6h，offset为3的因子
    df['_zero_time_factor'] = np.where(df['hour'] % 6 == 0, df['_factor1h'], np.nan)
    df[factor_name] = df['_zero_time_factor'].fillna(method='ffill')
    return df
